package edu.gatech.asthma.activity;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;
import edu.gatech.asthma.R;
import edu.gatech.asthma.database.DBAdapter;
import edu.gatech.asthma.ui_support.DatePickerFragment;
import edu.gatech.asthma.ui_support.HelpMenuHelper;
import edu.gatech.asthma.ui_support.ProfileCustomizer;
import edu.gatech.asthma.ui_support.SymptomsMenu;
import edu.gatech.asthma.ui_support.symptoms.*;

/**
 * Activity that allows the user to input any current or past asthma-related symptoms
 */
public class SymptomActivity extends FragmentActivity {
	int identifier = -1;
	DBAdapter database;
	ProfileCustomizer customizer;
	boolean younger; 	//a boolean value that tells if the child's age is less than 5 (or some cutoff)
	ArrayList<SymptomsListItem> symptoms;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			identifier = extras.getInt("ID");
		}

		getActionBar().setTitle("Record Symptoms");

		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_symptoms);

		database = new DBAdapter(this);
		database.open();
		customizer = new ProfileCustomizer(this,this.findViewById(android.R.id.content),database,identifier);
		customizer.setCustomizations();

		final ListView listView = (ListView) findViewById(R.id.symptoms_list);
		symptoms = new ArrayList<SymptomsListItem>();
		String [] symptomsList;

		//Populate the ListView depending on the age of the child
		//If under the age cutoff, change the title of the screen and the symptoms options
		if (database.getProfile(identifier).getAge() < getResources().getInteger(R.integer.age_cutoff)) {
			symptomsList = getResources().getStringArray(R.array.symptoms_younger);
			younger = true;
			((TextView) findViewById(R.id.symptoms_title)).setText(getString(R.string.symptoms_title_younger));
		}
		else {
			symptomsList = getResources().getStringArray(R.array.symptoms_older);
			younger = false;
		}
		for (int i=0; i<symptomsList.length; i++)	{
			symptoms.add(new SymptomsListItem(i,symptomsList[i]));
		}

		//create array adapter to handle the ListView
		SymptomsArrayAdapter adapter = new SymptomsArrayAdapter(this, R.layout.symptom_list_item,symptoms);
		listView.setAdapter(adapter);
		SymptomsClickListener listener = new SymptomsClickListener(younger, database.getProfile(identifier).getAccentColor());
		listView.setOnItemClickListener(listener);
		listView.setOnItemLongClickListener(listener);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		return SymptomsMenu.setUpSymptomMenu(menu, this);
	}

	/**
	 * Called when the "past" radio button is selected. (Linked from xml layout)
	 * Brings up a date/time picker dialog
	 */
	public void onPastButtonClicked(View v) {
		DialogFragment newFragment = new DatePickerFragment();
		newFragment.show(getSupportFragmentManager(), "datePicker");
	}

	/**
	 * Called when submit button is clicked. (Linked from xml layout)
	 */
	@SuppressLint("SimpleDateFormat")
	public void onSubmitClicked(View v) {
		String record = "";
		for(SymptomsListItem item : symptoms) {
			if(item.getIsChecked()) {
				record = record + "T";
			} else {
				record = record + "F";
			}
		}
		Log.d("Record", record);

		RadioButton now = (RadioButton)findViewById(R.id.symptoms_time_now);
		RadioButton past = (RadioButton)findViewById(R.id.symptoms_time_past);
		String time = "";

		if(now.isChecked()) {
			time = new SimpleDateFormat("MM-dd-yyyy").format(new Date());
		}
		if(past.isChecked()) {
			time = past.getText().toString().substring(13, past.getText().toString().length()-1);
		}

		database.addSymptomRecord(identifier, time, record);
		Intent i = new Intent(SymptomActivity.this, HomeScreenActivity.class);
		i.putExtra("ID", identifier);
		Toast.makeText(this, "Finish recording.", Toast.LENGTH_SHORT).show();
		SymptomActivity.this.startActivity(i);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		Intent i;
		switch (item.getItemId()) {
		case R.id.symptom_menu_show:
			if(database.hasSymptomRecords(identifier)){
				i = new Intent(SymptomActivity.this, ShowSymptomRecordsActivity.class);
				i.putExtra("ID", identifier);
				i.putExtra("Age", younger);
				SymptomActivity.this.startActivity(i);
			} else {
				Toast.makeText(this, "No records found.", Toast.LENGTH_SHORT).show();
			}
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}
	
	/**
	 * User clicked the help button on this screen
	 * @param v ImageView clicked
	 */
	public void helpButtonClicked(View v) {
		HelpMenuHelper.displayHelpMessage(this);
	}
}
